﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Lib.Common;
using SDT.Admin.Properties;
using SDT.Model;
using SDT.Lib.Paging;
using SDT.Admin.ViewModels;
using System.Linq.Dynamic;
using SDT.Lib.Helpers;

namespace SDT.Admin.Controllers
{
	[RoleHelper(Roles = RoleSystem.comment)]
	public class CommentsController : Controller
	{
		//
		// GET: /Comments/
		SDTDataContext eDataContext = new SDTDataContext();
		public ActionResult Index(int? page)
		{
			try
			{
					if (!page.HasValue) page = 1;
					string order = UserState.getUserStateFromRequest("Comment.order", "order", "ID").ToString();
					string orderDir = UserState.getUserStateFromRequest("Comment.orderDir", "orderDir", "desc").ToString();
					int currentPub = Convert.ToInt32(UserState.getUserStateFromRequest("Comment.TypePubYN", "TypePubYN", 0));
					string keyword = UserState.getUserStateFromRequest("Comment.Keyword", "Keyword", "").ToString();
					int pageSize = int.Parse(UserState.getUserStateFromRequest("Comment.pageSize", "PageSize", 20).ToString());
					var Product_Type = eDataContext.ProductTypes.ToList();
					var pubYNList = Utils.StringToDictionary(Settings.Default.CommentPubYN.ToString(), ",");
					var CommentList = eDataContext.Comments
					   .Where(cm => currentPub > 0 ? cm.PublishYN == (currentPub == 1 ? true : false) : true)
					   .Where(p => (string.IsNullOrEmpty(keyword) ? true :
									(p.ID.ToString().ToLower().Contains(keyword.ToLower().Trim())
									)))
									.OrderBy(string.Format("{0} {1}",order,orderDir))
									.ToPagedList(page.Value - 1, pageSize);

					ViewData["orderDir"] = orderDir;
					ViewData["order"] = order;

					return View(new CommentsViewModels
					{
						ListComment = CommentList,
						CurrentPubYN = currentPub,
						ListPubYN = pubYNList,
						Keyword = keyword
					});
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		#region Edit Comments
		public ActionResult Edit(int id)
		{
			try
			{
				if (id < 0) throw new NotSupportedException();
				var CurrentComment = eDataContext.Comments.Where(cm => cm.ID == id).SingleOrDefault();
				var CurrentArticle = eDataContext.CommentsInArticles.Where(ca => ca.CommentID == id).Select(c => c.Article).SingleOrDefault();
				return View(new EditCommentViewModels
				{
					Content = CurrentComment.CommentContent,
					Title = CurrentComment.Title,
					PublishYN = CurrentComment.PublishYN.HasValue ? CurrentComment.PublishYN.Value : false,
				});
			}
			catch (Exception ex)
			{
				ViewData["MessageInfo"] = "Thông tin bị lỗi bạn hãy kiểm tra lại";
				return View();
			}
		}
		[ValidateInput(false)]
		[HttpPost]
		public ActionResult Edit(int id, EditCommentViewModels cm)
		{
			try
			{
				if(id<0) throw new NotSupportedException();
				UpdateModel(cm);
				var CurrentComment = eDataContext.Comments.Where(c => c.ID == id).SingleOrDefault();
				CurrentComment.PublishYN = Request["PublishYN"].ToLower() != "false" ? true : false;
				CurrentComment.ModifiedBy = UserManager.GetUserId();
				CurrentComment.Title = cm.Title;
				CurrentComment.CommentContent = cm.Content;
				CurrentComment.ModifiedDate = DateTime.Now;
				eDataContext.SubmitChanges();
				return RedirectToAction("Index");
			}
			catch (Exception ex)
			{
				ViewData["MessageInfo"] = "Thông tin bị lỗi bạn hãy kiểm tra lại";
				return View();
			}
		}
		#endregion
		#region Read Comments
		public ActionResult Detail(int id)
		{
			try
			{
				if (id < 0) throw new NotSupportedException();
				var CurrentComment = eDataContext.Comments.Where(cm => cm.ID == id).SingleOrDefault();
				return View(new EditCommentViewModels { TheComment = CurrentComment});
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion

		#region Delete Comment
		public ActionResult Delete(int id)
		{
			try
			{
					if (id < 0) throw new NotSupportedException();
					var CurrentComment = eDataContext.Comments.Where(cm => cm.ID == id).SingleOrDefault();
					return View(CurrentComment);
			}
			catch (Exception ex)
			{ throw ex; }
		}

		[HttpPost]
		public ActionResult Delete(int id, FormCollection frm)
		{
			try
			{
				if (id < 0) throw new NotSupportedException();
				var CurrentCommentInProduct = eDataContext.CommentsInProducts.Where(cp => cp.CommentID == id).SingleOrDefault();
				if (CurrentCommentInProduct != null)
				{
					eDataContext.CommentsInProducts.DeleteOnSubmit(CurrentCommentInProduct);
					eDataContext.SubmitChanges();
				}
				var CurrentCommentInArticle = eDataContext.CommentsInArticles.Where(ca => ca.CommentID == id).SingleOrDefault();
				if (CurrentCommentInArticle != null)
				{
					eDataContext.CommentsInArticles.DeleteOnSubmit(CurrentCommentInArticle);
					eDataContext.SubmitChanges();
				}
                var CurrentCommentInVideo = eDataContext.CommentsInVideos.Where(ca => ca.CommentID == id).SingleOrDefault();
                if (CurrentCommentInVideo != null)
                {
                    eDataContext.CommentsInVideos.DeleteOnSubmit(CurrentCommentInVideo);
                    eDataContext.SubmitChanges();
                }
				var CurrentComment = eDataContext.Comments.Where(cm => cm.ID == id).SingleOrDefault();
				eDataContext.Comments.DeleteOnSubmit(CurrentComment);
				eDataContext.SubmitChanges();
				return RedirectToAction("Index");
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion
	}

	public static class ExtentionDataComent
	{
		public static Article NameIDArticle(this Comment cm)
		{
			try
			{
				SDTDataContext eDataContext = new SDTDataContext();
				if (cm.ID < 0) throw new NotSupportedException();
				var CommentArticle =eDataContext.CommentsInArticles.Where(c => c.CommentID == cm.ID).SingleOrDefault();
				if (CommentArticle != null)
				{
					var CurrentArticle = eDataContext.Articles.Where(a => a.ID == CommentArticle.ArticleID).SingleOrDefault();
				if (CurrentArticle != null)
					return CurrentArticle;
				return null;
				}
				else
				{return null;}
			}
			catch (Exception ex)
			{ throw ex; }
		}
	}
}
